package araxy.blog.service.impl;

import araxy.blog.entity.Token;
import araxy.blog.mapper.TokenMapper;
import araxy.blog.service.TokenService;
import araxy.blog.utils.DateUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class TokenServiceImpl implements TokenService {

    @Resource
    TokenMapper tokenMapper;

    @Override
    public String getTokenBySignature(String signature) {
        Token t = tokenMapper.selectBySignature(signature);
        return (t == null) ? null : t.getToken();
    }

    @Override
    @Transactional
    public Boolean insertToken(Token token) {
        token.setExpireTime(DateUtil.parseTime(new Date(DateUtil.getSysTimeMS() + DateUtil.getDaysMS(30)),"yyyy-MM-dd hh:mm:ss SSS"));
        return tokenMapper.insert(token) == 1;
    }

    @Override
    public List<Token> getTokensByOwner(String username) {
        return tokenMapper.selectByOwner(username);
    }
}
